package com.lmc.gobang.component;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
public class onlineUserManager {

    ConcurrentHashMap<String, WebSocketSession> sessions = new ConcurrentHashMap<>();

    public void online(String userId, WebSocketSession session) {
        if(sessions.get(userId)!=null){
            log.info("用户id"+userId+"的用户已登录,请勿重复登录");
            return;
        }
        sessions.put(userId, session);
        log.info("用户id"+userId+"的用户已上线");
    }

    public void offline(String userId, WebSocketSession session) {
        if(session == sessions.get(userId)){
            sessions.remove(userId);
        }
        log.info("["+userId+"]已下线");
    }
}
